package vn.edu.uit.service.fetchers;

import org.json.JSONObject;

import vn.edu.uit.hotelbooking.JsonParse.UserParser;
import vn.edu.uit.hotelbooking.model.User;
import vn.edu.uit.service.UITAPIContant;
import vn.edu.uit.service.UITHttpRequest;
import vn.edu.uit.service.UITHttpRequest.RequestMethod;
import vn.edu.uit.service.UITService;
import android.util.Log;

public class UserInfoFetcher {
    private static final String LOG_TAG = "UserInfoFetcher";
    private static UITHttpRequest httpRequest;
    
    /**
     * The method to get the user info
     * @return If success, return an User object. If fail, return null.
     */
    public static User getUserInfo() {
        UITService.REQUEST_SUCCESS = false;
        httpRequest = new UITHttpRequest(UITAPIContant.API_REST_DOMAIN_URL+UITAPIContant.Api_Functions.GET_USER_INFO);
        httpRequest.addParam(UITService.TAG_AUTHENTICATE, UITService.TOKEN);
        try {
            JSONObject object = httpRequest.doExecute(RequestMethod.POST);
            Log.d("JSON", object.toString());       
            if(object.getInt(UITService.TAG_ERROR) == 0) {     //request was successful, return an user object
                UITService.REQUEST_SUCCESS = true;
                return UserParser.getUserInfoFromJson(object.getJSONObject(UITService.TAG_DATA));
            } else {                                //request was failed, return null
                UITService.REQUEST_SUCCESS = false;
                UITService.MSG = object.getString(UITService.TAG_MSG);
                return null;
            }   
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(LOG_TAG, e.getMessage());
        }
        return null;
    }
    
    
}
